package com.lepu.app.fun.tool.dcb.bean;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.app.utils.DBHelper;

public class BeanToolDaiChan {
	private static final String TABLE_NAME = "ToolDaiChan";
	
	public int id;
	public String uid; 
    public String name; 
    public String number;
    public int done; // 0.未完成   1.已完成


    //添加
    public static boolean insert(BeanToolDaiChan bean) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
        if (dbHelp == null)
            return flag;

        ContentValues values = new ContentValues();
        values.put("uid", bean.uid);
        values.put("name", bean.name);
        values.put("number", bean.number);
        values.put("done", bean.done);
        flag = dbHelp.insert(TABLE_NAME, null, values);

        return flag;
    }
    
    //获取所有数据
    public static ArrayList<BeanToolDaiChan> getDataByUid(String uid) {
    	ArrayList<BeanToolDaiChan> provinces = new ArrayList<BeanToolDaiChan>();
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
		if (dbHelp == null)
			return provinces;

		SQLiteDatabase dbSqlite = null;
		try {
			dbSqlite = dbHelp.getWritableDatabase();

			Cursor queryResult = dbSqlite.query(TABLE_NAME, new String[] {"id","uid", "name", "number","done"}, 
					"uid=?",new String[] {uid}, null, null,null);

			cursorParse(provinces, queryResult);
			dbSqlite.close();
		} catch (Exception e) {
		}

		return provinces;
    }
    
    //获取所有数据
    public static ArrayList<BeanToolDaiChan> getDataByDone(String uid, int done) {
    	ArrayList<BeanToolDaiChan> provinces = new ArrayList<BeanToolDaiChan>();
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
		if (dbHelp == null)
			return provinces;

		SQLiteDatabase dbSqlite = null;
		try {
			dbSqlite = dbHelp.getWritableDatabase();

			Cursor queryResult = dbSqlite.query(TABLE_NAME, new String[] {"id","uid", "name", "number","done"}, 
					"uid=? and done=?",new String[] {uid,String.valueOf(done)}, null, null,null);

			cursorParse(provinces, queryResult);
			dbSqlite.close();
		} catch (Exception e) {
		}

		return provinces;
    }
    
    // 修改数据
    public static boolean update(BeanToolDaiChan bean) {

        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
        if (dbHelp == null)
            return flag;

        ContentValues values = new ContentValues();
        
        values.put("uid", bean.uid);
        values.put("name", bean.name);
        values.put("number", bean.number);
        values.put("done", bean.done);

        flag = dbHelp.update(TABLE_NAME, values, "uid=? and name=?", new String[]{bean.uid, bean.name});

        return flag;
    }
    
    
	//是否存在
	public static boolean isExist(String name) {
		ArrayList<BeanToolDaiChan> provinces = new ArrayList<BeanToolDaiChan>();
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
		if (dbHelp == null)
			return false;

		SQLiteDatabase dbSqlite = null;
		try {
			dbSqlite = dbHelp.getWritableDatabase();

			Cursor queryResult = dbSqlite.query(TABLE_NAME, new String[] {"id","uid", "name", "number","done"}, 
					"name=?", new String[] { name },null, null, null);
			cursorParse(provinces, queryResult);
			dbSqlite.close();
		} catch (Exception e) {
		}

		if (provinces.size() > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	//删除某一条记录
    public static boolean deleteItem(String uid, String name) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
        if (dbHelp == null)
            return flag;

        flag = dbHelp.delete(TABLE_NAME, "uid=? and name=?",new String[]{uid, name});

        return flag;
    }
	
	private static void cursorParse(ArrayList<BeanToolDaiChan> msgArray,Cursor cursor) {
		while (cursor.moveToNext()) {
			BeanToolDaiChan beanTarget = new BeanToolDaiChan();
			beanTarget.id = (cursor.getInt(cursor.getColumnIndex("id")));
			beanTarget.uid = (cursor.getString(cursor.getColumnIndex("uid")));
			beanTarget.name = (cursor.getString(cursor.getColumnIndex("name")));
			beanTarget.number = (cursor.getString(cursor.getColumnIndex("number")));
			beanTarget.done = (cursor.getInt(cursor.getColumnIndex("done")));
			msgArray.add(beanTarget);
		}
	}
	
}
